package com.example.consumerone.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.consumerone.services.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TestController {
    @Autowired
    private TestService testService;
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public Map hello(){
        System.out.println("输出："+testService.text());
        return testService.text();
    }
    @PostMapping("/text")
    public String text(){
        return "text";
    }
    @RequestMapping("/testGet")
    public String testGet(){
        return testService.testGet();
    }
//    @RequestMapping("/text")
//    public String text(){
//        return "text";
//    }

//    小程序，h5的get方式请求，后台使用@RequestParam注解进行接收参数
    @GetMapping("/consumerGet")
    public Map<String,Object> testGet(@RequestParam("text") String text){
//        json数据格式转换
        JSONObject json = JSONObject.parseObject(text);
//       键值对获取
        System.out.println(json.get("name"));
        System.out.println("跳过来了");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("message",testService.testGet());
        return map;
    }

//   小程序，h5的post方式请求，后台使用@RequestBody注解进行接收参数
    @PostMapping("/consumerPost")
    public Map<String,Object> test(@RequestBody String list){
        JSONObject jsonObject = JSON.parseObject(list);
        Map map01=(Map) jsonObject.get("text");
        System.out.println(map01.get("name"));
        System.out.println("跳过来了");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("message",testService.say());
        return map;
    }

    //fromData格式测试（）

}
